package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CategoryEntity> w = new QueryWrapper<>();
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                w
        );

        return new PageUtils(page);
    }

 /*   @Override
    public List<CategoryEntity> treeList() {
        //1.先查询所有的一级分类
        //extends ServiceImpl<CategoryDao
        //baseMapper 表示当前表的dao层对象
       //QueryWrapper 表示where关键字  qps
        QueryWrapper<CategoryEntity> w1 = new QueryWrapper<>();
        w1.eq("parent_cid",0);
        List<CategoryEntity> l1List = baseMapper.selectList(w1);
        //2.遍历所有的一级分类
        for(CategoryEntity l1 : l1List){
            //2.1 通过每个一级分类查询对应的二级分类集合
            Long catId = l1.getCatId();
            QueryWrapper<CategoryEntity> w2 = new QueryWrapper<>();
            w2.eq("parent_cid",catId);
            List<CategoryEntity> l2List = baseMapper.selectList(w2);
            l1.setChildren(l2List);
            //3.遍历所有二级分类 查询对应的三级分类
            for(CategoryEntity l2 : l2List){
                QueryWrapper<CategoryEntity> w3 = new QueryWrapper<>();
                w3.eq("parent_cid",l2.getCatId());
                List<CategoryEntity> l3List = baseMapper.selectList(w3);
                l2.setChildren(l3List);
            }
        }
        return l1List;
    }*/


    @Override
    public List<CategoryEntity> treeList() {
        //1. 和数据库只做一次交互
        List<CategoryEntity> allList = baseMapper.selectList(null);
        //2.从所有数据中筛选一级分类
        //函数式接口
        //item表示集合中的每个元素
        //filter 表示过滤
        //return表示条件
        //筛选结束之后应该收集
        //map整合处理
        List<CategoryEntity> l1List = allList.stream().filter(item -> {
            return item.getParentCid().equals(0L);
        }).map(l1 -> {
            //l1 查询对应的二级分类 l1是条件
            //二级分类集合 并且包含这三级分类
            List<CategoryEntity> children = getChildren(allList, l1);
            l1.setChildren(children);
            return l1;
        }).sorted((s1,s2)->{
            return (s1.getSort()==null?0:s1.getSort()) - (s2.getSort()==null?0:s2.getSort());
        }).collect(Collectors.toList());
        return l1List;
    }

    @Override
    public List<CategoryEntity> l1List() {

        QueryWrapper<CategoryEntity> w = new QueryWrapper<>();
        w.eq("parent_cid",0);
        return baseMapper.selectList(w);
    }

    @Override
    public Map<Long, List<Catelog2Vo>> getCatelog2List() {

        List<CategoryEntity> allList = baseMapper.selectList(null);

        Map<Long, List<Catelog2Vo>> map = allList.stream().filter(item -> {
            return item.getParentCid().equals(0L);
        }).collect(Collectors.toMap(k -> {
            //CatId是商品自己的id
            return k.getCatId();
        }, v -> {
            List<Catelog2Vo> collect1 = allList.stream().filter(item -> {
                return item.getParentCid().equals(v.getCatId());
            }).map(l2 -> {
                Catelog2Vo catelog2Vo = new Catelog2Vo();
                catelog2Vo.setCatalog1Id(l2.getParentCid());

                catelog2Vo.setId(l2.getCatId());
                catelog2Vo.setName(l2.getName());
                List<Catelog2Vo.Catelog3Vo> collect = allList.stream().filter(
                        item -> {
                            return item.getParentCid().equals(l2.getCatId());
                        }
                ).map(
                        l3 -> {
                            Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo();
                            catelog3Vo.setCatalog2Id(l3.getParentCid());
                            catelog3Vo.setId(l3.getCatId());
                            catelog3Vo.setName(l3.getName());
                            return catelog3Vo;
                        }
                ).collect(Collectors.toList());
                catelog2Vo.setCatalog3List(collect);
                return catelog2Vo;
            }).collect(Collectors.toList());
            return collect1;
        }));

        return map;
    }

    private List<CategoryEntity> getChildren(List<CategoryEntity> allList,CategoryEntity cate){
        List<CategoryEntity> childrenList = allList.stream().filter(item -> {
            return item.getParentCid().equals(cate.getCatId());
        }).map(c -> {
            List<CategoryEntity> children = getChildren(allList, c);
            c.setChildren(children);
            return c;
        }).sorted((s1,s2)->{
            return (s1.getSort()==null?0:s1.getSort()) - (s2.getSort()==null?0:s2.getSort());
        }).collect(Collectors.toList());
        return childrenList;
    }
}